﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing.Imaging;
using Microsoft.Expression.Encoder.Devices;
using WebcamControl;
using System.IO;

namespace Presentacion.Vistas
{
    /// <summary>
    /// Interaction logic for Video.xaml
    /// </summary>
    public partial class Video : Window
    {
        public Video()
        {
            InitializeComponent();
            string vidPath = @"C:\VideoClips";

            if (Directory.Exists(vidPath) == false)
            {
                Directory.CreateDirectory(vidPath);
            }

            var a = EncoderDevices.FindDevices(EncoderDeviceType.Video);

            WebCamCtrl.VideoDevice = EncoderDevices.FindDevices(EncoderDeviceType.Video)[0].Name;
            WebCamCtrl.AudioDevice = EncoderDevices.FindDevices(EncoderDeviceType.Audio)[0].Name;

            WebCamCtrl.VideoDirectory = vidPath;
            WebCamCtrl.VidFormat = VideoFormat.mp4;
            WebCamCtrl.FrameRate = 30;
            WebCamCtrl.FrameSize = new System.Drawing.Size(1280, 720);

            inicializarCamara();

        }

        private void inicializarCamara()
        {
            try
            {
                WebCamCtrl.StartCapture();
            }
            catch (Microsoft.Expression.Encoder.SystemErrorException ex)
            {
                MessageBox.Show("Device is in use by another application" + ex.Message);
            }
        }

        private void Countdown(int count, TimeSpan interval, Action<int> ts)
        {
            WebCamCtrl.StartRecording();
            var dt = new System.Windows.Threading.DispatcherTimer();
            dt.Interval = interval;
            dt.Tick += (_, a) =>
            {
                if (count-- == 0)
                {
                    WebCamCtrl.StopRecording();
                    WebCamCtrl.StopCapture();
                    dt.Stop();


                    Mensaje mensaje = new Mensaje("Gracias adrian te quiere", false, 3);
                    mensaje.WindowState = System.Windows.WindowState.Maximized;
                    mensaje.Closed += Volver_Principal;
                    mensaje.Show();

                }
                else
                    ts(count);
            };
            ts(count);
            dt.Start();
        }

        private void Empezar_Grabacion(object sender, System.EventArgs e)
        {
            Countdown(5, TimeSpan.FromSeconds(1), cur => tiempo.Content = cur.ToString());
        }

        private void Volver_Principal(object sender, EventArgs e)
        {
            Principal principal = new Principal();
            principal.Show();
            this.Close();
        }

        private void Iniciar_Popup(object sender, RoutedEventArgs e)
        {
            Mensaje mensaje = new Mensaje("el mundo se va a acabar en:", true, 5);
            mensaje.Closed += Empezar_Grabacion;

            mensaje.Show();
        }
    }
}
